<?php

/**
 * lay toan bo danh muc theo dieu kien
 * @param int $type kieu danh muc
 * @param int $cate_parent id danh muc cha
 * @return list danh muc va pham trang
 * @author anhmantk
 * @date Dec 26, 2011
 */
function getCategoryByType($cate_type = 1, $cate_parent = 0) {    
    $header = array(
        array('data' => t('Tên danh mục')), 
        array('data' => t('Sửa'), 'width' => '30'),
        array('data' => t('Tạo bộ lọc') , 'width' => '85'),         
        array('data' => t('Độ ưu tiên'), 'field' => 'cate_position', 'sort' => 'asc', 'width' => '81'),
        array('data' => t('Trạng thái'), 'width' => '85'),
        array('data' => t('Khóa'), 'width' => '30'),
    );
    // dieu kien loc
    $where = ' AND cate_parent = ' . $cate_parent;

    $query = 'SELECT * FROM {category} WHERE cate_type = ' . $cate_type . $where . ' ' . tablesort_sql($header);
    $count_query = 'SELECT COUNT(cate_id) FROM {category} WHERE cate_type = ' . $cate_type . $where;
    $result = pager_query($query, LIMIT_PAGER, 0, $count_query);
    $obj = null;
    while(($obj = db_fetch_object($result)) != null) {
        $rs[] = $obj;
    }

    $pager = theme('pager');

    return array($header, $rs, $pager);
}

/**
 * lay thong tin danh muc theo id
 * @param int $cate_id
 * @return type 
 */
function getCategoryById($cate_id) {
    $query = 'SELECT * FROM {category} WHERE cate_id = %d';
    return db_fetch_object(db_query($query, $cate_id));
}

/**
 * block danh muc
 * @param int $cate_id
 * @return boolean
 * @author anhmantk
 * @date Dec 27, 2011
 */
function blockCategoryById($cate_id) {
    $query = "UPDATE {category} SET cate_status = %d WHERE cate_id = %d";
    return db_query($query, STATUS_HIDE, $cate_id);
}

/**
 * them moi danh muc
 * @param array $params
 * @return boolean
 * @author anhmantk
 * @date Dec 27, 2011
 */
function insertCategoryData($params = array()) {
    $query = "INSERT INTO {category} (`cate_name`, `cate_message`, `cate_parent`,`cate_description`, `cate_keyword`, `cate_position`, `cate_status`, `cate_type`) VALUES ('%s', '%s', %d, '%s', '%s', %d, %d, %d)";
    return db_query($query, check_html($params['cate_name']), check_html($params['cate_message']), intval($params['cate_parent']), check_html($params['cate_description']), check_html($params['cate_keyword']), $params['cate_position'], $params['cate_status'], $params['cate_type']);    
}

/**
 * update thong tin danh muc
 * @param array $params
 * @param int $cate_id
 * @return boolean
 * @author anhmantk
 * @date Dec 27, 2011 
 */
function updateCategoryData($params = array(), $cate_id) {
    $query = "UPDATE {category} SET `cate_name` = '%s', `cate_message` = '%s', `cate_parent` = %d,`cate_description` = '%s', `cate_keyword` = '%s', `cate_position` = %d, `cate_status` = %d, `cate_type` = %d WHERE cate_id = %d";    
    return db_query($query, check_html($params['cate_name']), check_html($params['cate_message']), intval($params['cate_parent']), check_html($params['cate_description']), check_html($params['cate_keyword']), $params['cate_position'], $params['cate_status'], $params['cate_type'], $cate_id);
}

/**
 * kiem tra co con danh muc con hay khong
 * Neu la danh muc cuoi cung roi thi cho phep add cac thuoc tinh lien quan
 * @param int $cate_id
 * @return int tong so danh muc con
 * @author anhmantk
 * @date Dec 27, 2011
 */
function getCountSubCate($cate_id) {
    $query = "SELECT COUNT(cate_id) FROM {category} WHERE cate_parent = %d";
    return db_result(db_query($query, $cate_id));
}